/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yokeyword.sample.demo_flow.ui.fragment.home;

import hollowsoft.slidingdrawer.SlidingDrawer;

import me.yokeyword.sample.ResourceTable;
import me.yokeyword.sample.demo_flow.adapter.HomeAdapter;
import me.yokeyword.sample.demo_flow.adapter.SliderAdapter;
import me.yokeyword.sample.demo_flow.base.MySupportFraction;
import me.yokeyword.sample.entity.Article;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.utils.PacMap;

import java.util.ArrayList;
import java.util.List;

import static me.yokeyword.sample.util.ResUtil.getStrArray;


public class HomeFraction extends MySupportFraction {
    private String[] mTitles;
    private String[] mContents;
    private SlidingDrawer slidingDrawer;
    private ListContainer mRecy, sliderList;
    private HomeAdapter mAdapter;
    Text mTxtAccountName;

    private HomeFraction() {
    }

    public static HomeFraction newInstance() {
        return new HomeFraction();
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_layout_home, container, false);
        mRecy = (ListContainer) component.findComponentById(ResourceTable.Id_lc_container);
        initComponent(component);
        initNavigationSlider(component);
        return component;
    }

    private void initComponent(Component component) {
        mTitles = getStrArray(component.getContext(), ResourceTable.Strarray_array_title);
        mContents = getStrArray(component.getContext(), ResourceTable.Strarray_array_content);
        mAdapter = new HomeAdapter(component.getContext());
        mAdapter.setClickListener((listContainer, component1, i, l) -> start(DetailFraction.newInstance(mAdapter.getTitle(i))));

        List<Article> articleList = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            int index = (int) (Math.random() * 3);
            Article article = new Article(mTitles[index], mContents[index]);
            articleList.add(article);
        }
        mAdapter.setDatas(articleList);
        mRecy.setItemProvider(mAdapter);
    }

    void initNavigationSlider(Component component) {
        sliderList = (ListContainer) component.findComponentById(ResourceTable.Id_list_slider);

        SliderAdapter sliderAdapter = new SliderAdapter(component.getContext());
        sliderAdapter.setClickListener((listContainer, component1, i, l) -> {
            switch (i) {
                case 0:
                    slidingDrawer.close();
                    break;
                case 1:
                    start(LoginFraction.newInstance());
                    break;
            }
        });

        List<Article> articleList = new ArrayList<>();
        articleList.add(new Article("Home", ""));
        articleList.add(new Article("Sign In", ""));

        sliderAdapter.setData(articleList);
        sliderList.setItemProvider(sliderAdapter);
        slidingDrawer = (SlidingDrawer) component.findComponentById(ResourceTable.Id_slide);
        mTxtAccountName = (Text) component.findComponentById(ResourceTable.Id_TxtAccountName);
        component.findComponentById(ResourceTable.Id_handle).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (slidingDrawer.isOpened()) {
                    slidingDrawer.close();
                } else {
                    slidingDrawer.open();
                }
            }
        });
    }

    /**
     * OnNewIntent() similar to Ability
     * @param args Bundle data
     */
    @Override
    public void onNewBundle(PacMap args) {
        super.onNewBundle(args);
        mTxtAccountName.setText(args.getString("UserName"));
    }
}
